package com.lzp.controller.center;

import com.github.pagehelper.PageHelper;
import com.lzp.controller.BaseController;
import com.lzp.pojo.OrderItems;
import com.lzp.pojo.Orders;
import com.lzp.pojo.bo.center.OrderItemsCommentBO;
import com.lzp.service.center.MyCommentsService;
import com.lzp.service.center.MyOrdersService;
import com.lzp.utils.IMOOCJSONResult;
import com.lzp.utils.PagedGridResult;
import com.lzp.utils.ServerResponse;
import com.lzp.utils.enums.Yes0rNo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dtyy
 * @date 2020/10/12
 */
@Api(value = "用户中心评价模块", tags = "用户中心评价模块")
@RestController
@RequestMapping("mycomments")
public class MyCommentsController extends BaseController {

    @Autowired
    private MyOrdersService myOrdersService;

    @Autowired
    private MyCommentsService myCommentsService;

    @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
    @PostMapping("/pending")
    public ServerResponse pending(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId) {

        // 判断用户和订单是否关联
        ServerResponse checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }
        // 判断该笔订单是否已经评价过，评价过了就不再继续
        Orders myOrder = (Orders) checkResult.getData();
        if (myOrder.getIsComment() == Yes0rNo.YES.getCode()) {
            return ServerResponse.createByErrorMsg("该笔订单已经评价");
        }

        List<OrderItems> list = myCommentsService.queryPendingComment(orderId);

        return ServerResponse.createBySuccess(list);
    }

    @ApiOperation(value = "查询评价", notes = "查询评价", httpMethod = "POST")
    @PostMapping("/query")
    public ServerResponse query(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @RequestParam Integer page,
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(userId)) {
            return ServerResponse.createByErrorMsg("用户id为空");
        }

        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult result = myCommentsService.queryMyComments(userId, page, pageSize);

        return ServerResponse.createBySuccess(result);
    }

    @ApiOperation(value = "保存评论列表", notes = "保存评论列表", httpMethod = "POST")
    @PostMapping("/saveList")
    public ServerResponse saveList(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @ApiParam(name = "commentList", value = "订单商品评论列表", required = true)
            @RequestBody List<OrderItemsCommentBO> commentList) {
        // 判断用户和订单是否关联
        ServerResponse checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }
        // 校验评论列表不为空
        if (commentList == null || commentList.isEmpty()) {
            return ServerResponse.createByErrorMsg("评论内容不能为空");
        }
        myCommentsService.saveComments(orderId, userId, commentList);

        return ServerResponse.createBySuccess();
    }

    /**
     * 用于验证用户和订单是否有关联关系，避免非法用户调用
     *
     * @return
     */
    public ServerResponse checkUserOrder(String userId, String orderId) {
        Orders order = myOrdersService.queryMyOrder(userId, orderId);
        if (order == null) {
            return ServerResponse.createByErrorMsg("订单不存在！");
        }
        return ServerResponse.createBySuccess(order);
    }

}
